var objCommandee = {
    getCommand:
    function(msg)
    {
        return msg[0]+msg[1];
    },

    setPlayer:
    function(id)
    {
        return id.lpad("0", 4);
    },
    
    getPlayer:
    function(msg)
    {
        return msg[2]+msg[3]+msg[4]+msg[5];
    },
    
    getMessage:
    function(msg)
    {
        return msg.substr(6);
    },
    
    setAllPlayers:
    function(msg)
    {
        var players = msg.split(',');
        var id      = null;
        var name    = null;
        for (var i = 0; i < players.length; ++i)
        {
            id      = players[i].substr(0, 4);
            name    = players[i].substr(4);
            if (id == objGame.my_id) continue;
            
            // add user to the users list
            $('#usersList').append($("<option></option>").attr("value", id).text(name));
        }
        $('#usersList').sortSelectList();
    },
    
    playerDisconnect:
    function(id)
    {
        // later if this player its in a game, it will be sucks...

        $('#usersList option[value="'+id+'"]').remove();
    },

    gameRequest:
    function(_this, socket, id)
    {
        socket.send(objCommands.GAME_REQUEST +
                    _this.setPlayer(objGame.my_id) +
                    _this.setPlayer(id));
    },
    
    gameOver:
    function(_this)
    {
        $('#usersList').removeAttr("disabled");
        objGame.init(objGame, 0);
    },

    sendChatMsg:
    function(_this, socket, msg)
    {
        socket.send(objCommands.CHAT_MSG + _this.setPlayer(objGame.my_id) + msg);
    },
    
    process:
    function(_this, msg, socket)
    {
        switch (_this.getCommand(msg))
        {
            case objCommands.SET_ID:
            {
                objGame.my_id = _this.getPlayer(msg);
            }
            break;
            
            case objCommands.SET_NEW_PLAYER:
            {
                // add user to the users list
                $('#usersList').append($("<option></option>").
                    attr("value", _this.getPlayer(msg)).text(_this.getMessage(msg)));
                $('#usersList').sortSelectList();
            }
            break;
            
            case objCommands.SET_ALL_PLAYERS:
            {
                _this.setAllPlayers(_this.getMessage(msg));
            }
            break;
            
            case objCommands.PLAYER_DISCONNECT:
            {
                _this.playerDisconnect(_this.getPlayer(msg));

                if (objGame.step_type !== null &&
                    objGame.game_over === false &&
                    objGame.opp_id.toString() === _this.getPlayer(msg))
                {
                    $('#message').text('Your oppenent has leave the match :/');
                    objGame.game_over = true;
                }
            }
            break;

            case objCommands.GAME_REQUEST:
            {
                apprise('Wanna play with ' + _this.getMessage(msg) + '?',
                        {'verify':true, 'animate':true},
                        function (r)
                        {
                            if (r)
                            {
                                objGame.opp_name = _this.getMessage(msg);

                                socket.send(objCommands.GAME_REQUEST_ACCEPT +
                                            _this.getPlayer(msg));
                            }
                            else
                            {
                                socket.send(objCommands.GAME_REQUEST_DECLINE +
                                            _this.setPlayer(objGame.my_id));
                            }
                        });
            }
            break;
            
            case objCommands.GAME_START:
            {
                _this.gameOver(objCommandee);
                
                $('#usersList').attr("disabled", "disabled");
                objGame.opp_id      = _this.getMessage(msg).substr(1, 4);
                objGame.opp_name    = _this.getMessage(msg).substr(5);
                objGame.init(objGame, _this.getMessage(msg).substr(0,1));
                objGame.startGame(objGame);
                
                $('#chatMsg').focus();
                $("#usersList option:selected").removeAttr("selected");
            }
            break;
            
            case objCommands.GAME_OVER:
            {
                objGame.game_over = true;
                $('#usersList').removeAttr("disabled");
            }
            break;
            
            case objCommands.GAME_STEP:
            {
                objGame.getStep(objGame,
                                parseInt(_this.getMessage(msg).substr(0, 2), 10),
                                parseInt(_this.getMessage(msg).substr(2, 2), 10));
            }
            break;
            
            case objCommands.GAME_WON:
            {
                objGame.game_over = true;
                $('#usersList').removeAttr("disabled");
                $('#message').text('You\'ve won the match :]');
            }
            break;
            
            case objCommands.GAME_LOST:
            {
                objGame.game_over = true;
                $('#usersList').removeAttr("disabled");
                $('#message').text('You\'ve lost the match :/');
            }
            break;

            case objCommands.CHAT_MSG:
            {
                var player_name = _this.getMessage(msg).substring(0, _this.getMessage(msg).indexOf(':', 0));
                var chat_msg    = _this.getMessage(msg).substr(_this.getMessage(msg).indexOf(':', 0) + 1);

                $("#chatContent").prepend("<div><b>"+player_name+":</b> "+chat_msg+"</div>");
            }
            break;

            case objCommands.SERVER_IS_FULL:
            {
                $('#content').css('display', 'none');
                objWebSocket.server_is_full = true;
                apprise('Server is Full!', {'animate':true});
            }
            
            case objCommands.GAME_WON_SPOTS:
            {
                var type    = _this.getMessage(msg).substr(0, 1);
                var ct      = _this.getMessage(msg).substr(1);
                
                objGame.
                drawFiveInARow( objGame, type, objGame.step_type,
                                {   
                                    'p1x': ct.substr(0,2), 'p1y': ct.substr(2,2),
                                    'p2x': ct.substr(4,2), 'p2y': ct.substr(6,2)
                                });
            }
            break;
            
            case objCommands.GAME_LOST_SPOTS:
            {
                type    = _this.getMessage(msg).substr(0, 1);
                ct      = _this.getMessage(msg).substr(1);
                
                objGame.
                drawFiveInARow( objGame, type, objGame.step_type === 1 ? 0 : 1,
                                {   
                                    'p1x': ct.substr(0,2), 'p1y': ct.substr(2,2),
                                    'p2x': ct.substr(4,2), 'p2y': ct.substr(6,2)
                                });
            }
            break;

            default:
            {
            }
            break;
        }
    }
};
